<?php
    session_start();
    include 'conecta_bd.php';
    if(empty($_SESSION['usuario']))
    {
        header('Location: index.html');
    }
    else
    {
    $usr_usuario=$_SESSION['usuario'];           
    $query=mysql_query("SELECT email,username,pwd FROM cliente WHERE username='$usr_usuario'");
    $row=  mysql_fetch_array($query);
    $userV = $row['username'];
    $correoV = $row['email'];
    $passV = $row['pwd'];
    }
    
    //coreroN -- Correo Nuevo    correoV -- correo Viejo
    //userN -- username Nuevo    userV -- username Viejo
    $correoN = $_POST['email'];
    $userN = $_POST['nameUser'];
    $passN = $_POST['pass'];
    
    if($correoN == $correoV && $userN==$userV && $passN == $passV) //No cambia nada
    {
        header('Location: ../cuenta.php?m=nada');
    }
    else if($correoN == $correoV && $userN==$userV) //Actualiza password
    {
            $consulta=mysql_query("update cliente set pwd='$passN' where username='$userV'");  
            if($consulta){
                header('Location: ../index.html?m=cambioCuenta');
            }else{
                echo "Error inesperado no se ha podido modificar cuenta";
            }
    }
    else if($correoN == $correoV) //cambio user
    {
        if(($total = mysql_num_rows(mysql_query('SELECT cliente_id FROM cliente WHERE username="'.$_POST['nameUser'].'"'))) > 0){
            header('Location: ../cuenta.php?m=USRrepe');
        }
        else
        {
            $consulta=mysql_query("update cliente set username='$userN', pwd='$passN' where username='$userV'");  
            if($consulta)
            { 
                header('Location: ../index.html?m=cambioCuenta'); 
            }
            else
            {
           echo "Error inesperado no se ha podido modificar cuenta"; 
            }
        }
    }
    
    else if($userN==$userV) //cambia correo
    {
        if(($total = mysql_num_rows(mysql_query('SELECT cliente_id FROM cliente WHERE email="'.$_POST['email'].'"'))) > 0)
        {
            header('Location: ../cuenta.php?m=EMArepe');
        }
        else
        {
            $consulta=mysql_query("update cliente set email='$correoN', pwd='$passN' where username='$userV'");  
            if($consulta)
            {
                header('Location: ../index.html?m=cambioCuenta');
            }
            else
            {
                echo "Error inesperado no se ha podido modificar cuenta";
            }
        }
    }
    else //Actaliza todo
    {
        if(($total = mysql_num_rows(mysql_query('SELECT cliente_id FROM cliente WHERE username="'.$_POST['nameUser'].'"'))) > 0)
        {
            header('Location: ../cuenta.php?m=USRrepe');
        }
        else if(($total = mysql_num_rows(mysql_query('SELECT cliente_id FROM cliente WHERE email="'.$_POST['email'].'"'))) > 0)
        {
            header('Location: ../cuenta.php?m=EMArepe');
        }
        else
        {
            $consulta=mysql_query("update cliente set username='$userN', email='$correoN', pwd='$passN' where username='$userV'");  
            if($consulta)
            {
                header('Location: ../index.html?m=cambioCuenta');
            }
            else
            {
                echo "Error inesperado no se ha podido modificar cuenta";
            }
        }
    }
           

  ?>

